package com.xyq.fs.views;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;

import com.xyq.fs.base.MyDirectoryS;
import com.xyq.fs.constants.FieldConstant;
import com.xyq.fs.constants.ParseConstant;
import com.xyq.fs.contants.filesuffixes2.ExcelEnums;
import com.xyq.fs.contants.filesuffixes2.PdfEnums;
import com.xyq.fs.contants.filesuffixes2.PptEnums;
import com.xyq.fs.contants.filesuffixes2.TxtEnums;
import com.xyq.fs.contants.filesuffixes2.WordEnums;

import com.xyq.fs.index.IndexFileInf;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.ImageCursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.web.HTMLEditor;

import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class WindowsD extends Application {

	HTMLEditor htmlEditor = new HTMLEditor();
    static ImageCursor Mouse = new ImageCursor(WindowsHelp.getsb(), 0, 0);
	Document doc;
	Path path;
	VBox root = new VBox();
	public WindowsD(Document doc) {

		this.doc = doc;

	}

	public void setText(String text) {

		htmlEditor.setHtmlText(text);
	}

	@Override
	public void start(Stage stage) {

		stage.setResizable(false);
		stage.getIcons().add(WindowsHelp.getImg());
		/***
		 * 界面关闭事件
		 */
		stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
			@Override
			public void handle(WindowEvent event) {

				htmlEditor = null;
				stage.close();
			}
		});
		String ps = doc.get("FILE_NAME");
		stage.setTitle(ps + "(文件笔记)");
		stage.setWidth(770);
		stage.setHeight(550);
		Scene scene = new Scene(root);
		root.getStylesheets().add(WindwosA.class.getResource("WindowsB.css").toExternalForm());
		root.setPadding(new Insets(8, 8, 8, 8));
		root.setSpacing(5);
		root.setAlignment(Pos.BOTTOM_LEFT);

		Button btn = new Button("更新文件笔记");
		btn.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {

				

				new Thread(() -> {
					
					String txt = htmlEditor.getHtmlText();
					String fname = ps.toUpperCase();
					IndexFileInf iff = null;
					if (TxtEnums.suffixContains(fname))
						iff = ParseConstant.itf;
					else if (fname.endsWith(PdfEnums.PDF)) {
						iff = ParseConstant.ipf;
					} else if (PptEnums.suffixContains(fname)) {
						iff = ParseConstant.ippf;
					} else if (WordEnums.suffixContains(fname)) {
						iff = ParseConstant.iwf;
					} else if (ExcelEnums.suffixContains(fname)) {
						iff = ParseConstant.ief;
					}else iff = ParseConstant.ifn;

				
					Path path = Paths.get(doc.get(FieldConstant.FILE_PATH));
					System.out.println(path);
					iff.indexFile(path, txt);
					synchronized (MyDirectoryS.reader) {
						
						MyDirectoryS.commit();
						MyDirectoryS.updateReaderAndSearcher();
					}

					// 备份这个文件
					try {
						Path tar = Paths
								.get(System.getProperty("user.dir") + "\\CONF\\BACKUP\\" + path.getFileName() + "");
						tar.getParent().toFile().mkdirs();
						Files.copy(path, tar, StandardCopyOption.REPLACE_EXISTING);
					} catch (Exception e) {
						e.printStackTrace();
						System.out.println("文件无法备份");
					}
					WindowsB.cacheMyFile.clear();
				}).start();
				
				MyAlert.alert("笔记修改完毕");
			}
		});
		Button btn2 = new Button("删除文件笔记");
		HBox hBox2 = new HBox();
		hBox2.setPadding(new Insets(5, 5, 0, 0));

		Text t1 = new Text("  (提示: 点击更新或删除按钮后,还有一次撤回的机会。\n  具体操作: 直接点击更新或点击查看笔记（显示为更新前内容）后点击更新即可恢复)");
		hBox2.getChildren().addAll(btn, btn2, t1);
		btn2.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {

				new Thread(() -> {

					String fname = ps.toUpperCase();
					IndexFileInf iff = null;
					if (TxtEnums.suffixContains(fname))
						iff = ParseConstant.itf;
					else if (fname.endsWith(PdfEnums.PDF)) {
						iff = ParseConstant.ipf;
					} else if (PptEnums.suffixContains(fname)) {
						iff = ParseConstant.ippf;
					} else if (WordEnums.suffixContains(fname)) {
						iff = ParseConstant.iwf;
					} else if (ExcelEnums.suffixContains(fname)) {
						iff = ParseConstant.ief;
					}else iff = ParseConstant.ifn;
					Path path = Paths.get(doc.get(FieldConstant.FILE_PATH));

					Path tar = Paths.get(System.getProperty("user.dir") + "\\CONF\\BACKUP\\" + path.getFileName() + "");
					if (!Files.exists(path)) {
						try {
							MyDirectoryS.writer.deleteDocuments(new Term(FieldConstant.FILE_PATH, path.toString()));
						} catch (Exception e) {
							e.printStackTrace();
						}
						iff.indexFile(tar, null);
					} else
						iff.indexFile(path, null);

					synchronized (MyDirectoryS.reader) {
						
						MyDirectoryS.commit();
						MyDirectoryS.updateReaderAndSearcher();
					}
					WindowsB.cacheMyFile.clear();
				}).start();
				MyAlert.alert("笔记删除成功");
			}
		});
		stage.show();
		htmlEditor.setHtmlText(doc.get(FieldConstant.FILE_BZ_SORT));
		htmlEditor.setPrefHeight(990);
		root.getChildren().add(hBox2);

		root.getChildren().addAll(htmlEditor);
		scene.setRoot(root);
		
		// 在scene中修改光标
		scene.setCursor(Mouse);
		stage.setScene(scene);
		
	}

	public static void main(String[] args) {
		launch(args);
	}
}